package dcl.software.filesync.server.auth;

import javax.annotation.Resource;

import org.apache.http.HttpException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "authorization")
public class AuthorizationController {
	@Resource
	private AuthorizationService authService;
	
	@RequestMapping(value = "createaccount", method = RequestMethod.POST)
	@ResponseBody
	public String createAccount(@RequestBody UserDTO userDTO) throws HttpException {
		verify(userDTO);
		User user = new User(userDTO);
		if (user.getPassword() == null)
			throw new AuthorizationException("Hashed encoding failed. Please speak with the admin.");
		return authService.createAccount(user);
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public String login(@RequestBody UserDTO userDTO) throws HttpException
	{
		return authService.login(new User(userDTO));
	}

	@RequestMapping(value = "testaspect", method = RequestMethod.GET)
	@ResponseBody
	public String testAspect(User user, @RequestHeader("authToken") String header) throws HttpException {
		user = authService.getUserFromToken(header);
		return user.getUserName();
	}

	@RequestMapping(value = "testget", method = RequestMethod.GET)
	@ResponseBody
	public UserDTO testGet(User user, @RequestHeader("authToken") String header) throws HttpException {
		UserDTO userDTO = new UserDTO();
		user.setUserName("dloutsch");
		return userDTO;
	}

	@RequestMapping(value = "testexception", method = RequestMethod.GET)
	@ResponseBody
	public String testException()
			throws HttpException {
		verify(null);
		throw new AuthorizationException("invalid Username");
	}

	/*
	 * @ExceptionHandler(AuthorizationException.class) public void
	 * handleIOOnlineChargesException(final AuthorizationException ex, final
	 * HttpServletResponse response) { try {
	 * response.getOutputStream().print(ex.getMessage());
	 * response.getOutputStream().flush(); response.getOutputStream().close();
	 * response.setStatus(HttpStatus.I_AM_A_TEAPOT.value()); } catch
	 * (IOException e) { throw new RuntimeException(
	 * "Exception occurred in WorkProductTokenController.handleIOException: " +
	 * ex.getMessage()); } // } }
	 */

	private void verify(UserDTO createDTO) throws HttpException {
		String userName = createDTO.getUserName();
		String password = createDTO.getPassword();
		String conPassword = createDTO.getConfirmedPassword();
		String email = createDTO.getEmail();
		if(userName==null || userName.length()<8){
			throw new AuthorizationException("userName must be at least 8 characters long.");
		}
		if(password==null || password.length()<8){
			throw new AuthorizationException("password must be at least 8 characters long.");
		}
		if(!password.equals(conPassword)){
			throw new AuthorizationException("confirmation password must match.");
		}
		if(email==null || !email.contains("@")){
			throw new AuthorizationException("email must contain an '@'.");
		}
	}
}
